package
{
      import com.pblabs.engine.PBE;
      import com.pblabs.engine.components.TickedComponent;
      import com.pblabs.engine.entity.IEntity;
      import com.pblabs.engine.entity.PropertyReference;

      import flash.geom.Point;

      public class EnemyManagerLogicComponent  extends TickedComponent
      {
            // Used to define the remaining time that has to elapse
            // before the next enemy Entity is created
            protected var timeToNextEnemy:Number = 0;
            // A collection of Template names that will be
            // used to construct new enemies
            public var templateNames:Array = new Array();
            // The time the has to elapse between new enemies
            public var timeBetweenEnemies:Number = 10;
            // The property of the enemy Entities that
            // defines their position
            public var positionReference:PropertyReference;
            // The property of the enemy Entities that
            // defines their size
            public var sizeReference:PropertyReference;

            public function EnemyManagerLogicComponent()
            {

            }

            // The onTick() function is called once per frame
            public override function onTick(tickRate:Number):void
            {
                  // Count down the timeToNextEnemy timer.
                  timeToNextEnemy -= tickRate;

                  // If the timeToNextEnemy timer has reached zero...
                  if (timeToNextEnemy <= 0 && templateNames.length > 0)
                  {
                        // ...reset it
                        timeToNextEnemy = timeBetweenEnemies;

                        // Get a random index to the templateNames Array
                        var randomArrayIndex:int =
                              Math.round(Math.random() * (templateNames.length - 1));

                        // Create a new Entity using a randomly select
                        // Template name from the templateNames Array
                        var enemyEntity:IEntity = PBE.templateManager.instantiateEntity(
                              templateNames[randomArrayIndex]);

                        // enemyEntity will be null if the values in the templateNames
                        // Array don't match the names of any Templates defined in
                        // the XML file
                        if (enemyEntity != null)
                        {
                              // Get the size of the Entity
                              var size:Point = enemyEntity.getProperty(sizeReference) as Point || new Point();

                              // Create a Point that will place the Entity
                              // randomly along the width of the stage, and
                              // just above the top
                              var startingPosition:Point = new Point(
                                    Math.random() * PBE.mainStage.stageWidth,
                                    -size.y / 2);

                              // Assign the new position to the Entity
                              enemyEntity.setProperty(positionReference, startingPosition);
                        }
                  }
            }
      }
}
